//
//  HMStrongBigCategoryView.m
//  优学荟管理系统
//
//  Created by 陈辉明 on 2017/11/30.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "HMStrongBigCategoryView.h"
#import "HMStrongBigCategoryCell.h"


@interface HMStrongBigCategoryView()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (assign, nonatomic) NSIndexPath *selIndex;//单选，当前选中的行
@property(nonatomic,strong) NSMutableArray *dataSource;
@end

#define cellID @"HMStrongBigCategoryCellId"

@implementation HMStrongBigCategoryView

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setTableView];
  
   
   
}
//-(instancetype)initWithFrame:(CGRect)frame with:(BOOL)isHomework
//{
//    if (self=[super initWithFrame:frame]) {
////       self = [[[NSBundle mainBundle]loadNibNamed:@"HMStrongBigCategoryView" owner:nil options:nil]lastObject];
//        if (isHomework) {
//            self.isHomework = isHomework;
//            [self setTableView];
//           __weak typeof(self) weakSelf = self;
//                self.getHomeData = ^(NSMutableArray *dataArr) {
//                    weakSelf.dataSource = dataArr;
//                    [weakSelf.tableView reloadData];
//                };
//          
//        }
//        else
//        {
//            [self setStrongBigNetWork];
//        }
//    }
//   
//    
//    return self;
//}

- (void)setTableView {
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 60;
    [self.tableView registerNib:[UINib nibWithNibName:@"HMStrongBigCategoryCell" bundle:nil] forCellReuseIdentifier:cellID];
   
}


#pragma mark - 请求加强课大类目接口
- (void)setStrongBigNetWork{
    //    http://192.168.0.226:8080/StarShine/ys/lesson.do&action=getLessonCategory
    NSString *url = [NSString stringWithFormat:@"lesson.do?action=getLessonCategory"];
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    [MBProgressHUD showHUD];
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        [MBProgressHUD hideHUD];
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            for (NSDictionary *dict in [Obj valueForKey:@"t"]) {
                
                HMStrongBigCategoryModel *model = [HMStrongBigCategoryModel HMStrongBigCategoryModelWithDict:dict];
                [self.dataSource addObject:model];
            }
            [self.tableView reloadData];
        }
        
    } Fail:^(NSError *Error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:NetErr];
    }];
    
}


#pragma mark - 数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HMStrongBigCategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    
    cell.model = self.dataSource[indexPath.row];
    
   
    
    //当上下拉动的时候，因为cell的复用性，我们需要重新判断一下哪一行是打勾的
    if (_selIndex == indexPath) {
//        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.selectButton.selected = YES;
    }else {
//        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectButton.selected = NO;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //之前选中的，取消选择
    HMStrongBigCategoryCell *celled = [tableView cellForRowAtIndexPath:_selIndex];
    celled.selectButton.selected = NO;
    //记录当前选中的位置索引
    _selIndex = indexPath;
    //当前选择的打勾
    HMStrongBigCategoryCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selectButton.selected = YES;
}




-(NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}

#pragma mark - 点击事件

- (IBAction)didRemoveButton:(id)sender {
    [self removeFromSuperview];
    _selIndex = nil;
}

- (IBAction)clickSureButton:(id)sender {
    
    if (_selIndex == nil) {
    }

    else
    {
        if (self.selectRowBlock && _selIndex != nil) {
            self.selectRowBlock(self.dataSource[self.selIndex.row]);
        }
    }
   
    [self removeFromSuperview];
    
}






@end
